package com.example.bootdemo.controller;

import com.example.bootdemo.configuration.RabbitMqConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbitmq")
public class RabbitMessageController {

    private static Logger logger = LoggerFactory.getLogger(Logger.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/direct")
    public String sendDirectMessage(@RequestParam("message") String message){

        logger.info("message={}",message);
        rabbitTemplate.convertAndSend("direct.key",message);

        return "发送直接消息："+message;
    }

    @GetMapping("/topic/xiaomi")
    public String sendTopicMessageXiaoMi(@RequestParam("message") String message){

        logger.info("message={}",message);

        rabbitTemplate.convertAndSend("xiaomi.news",message);

        return "发送小米消息："+message;
    }

    @GetMapping("/topic/huawei")
    public String sendTopicMessageHuaWei(@RequestParam("message") String message){

        logger.info("message={}",message);

        rabbitTemplate.convertAndSend("huawei.news",message);

        return "发送华为消息:"+message;
    }

    @GetMapping("/topic/iphone")
    public String sendTopicMessageIphone(@RequestParam("message") String message){

        logger.info("message={}",message);

        rabbitTemplate.convertAndSend("us.iphone.news",message);

        return "发送 iphone 消息："+message;
    }


}
